#include "BitShifter.h"


BitShifter::BitShifter()
{
	direction = ShiftLeft;
	value = 0;
}

BitShifter::BitShifter(unsigned char d)
{
	direction = d;
	value = 0;
}

void BitShifter::set(unsigned char v)
{
	value = v;
}

unsigned char BitShifter::get()
{
	return value;
}

void BitShifter::shiftIn(unsigned char bit)
{

	if(direction == ShiftLeft) //shift left
	{ //first bit 0
		value = value << 1;
		value = SetBit(value, 0, bit);
	}
	else //shift right
	{
		value = value >> 1;
		value = SetBit(value, 7, bit);
	}
}

unsigned char BitShifter::shiftOut()
{
	unsigned char bit = 0;
	if(direction == ShiftLeft)//shift left
	{
		bit = GetBit(value, 7); //7th bit
		value = value << 1;
	}
	else //shift right.
	{
		bit = GetBit(value, 0); // 0th bit
		value = value >> 1;
	}
	return bit;
}

unsigned char BitShifter::operator[](unsigned char index)
{
	if(index >= 0 && index < 8)
	{
		return GetBit(value, index);
	}
	else
		return 255; //as an error.
}


